<template>
    <el-dialog :visible.sync="isShow" :title="headerTitle" @close="handleClose" center top="" width="1000px" append-to-body :fullscreen="fullscreen">
        <el-form :model="dialogForm" size="small" label-position="top" label-suffix=":" ref="detailForm" :rules="rules" label-width="80px">
            <el-row>
                <el-col>
                    <el-form-item :label="'索引类型'" prop="dictType">
                        <el-input v-model="dialogForm.dictType" :placeholder="$t('public.placeholder',{placeholder: '索引类型'})" size="small" />
                    </el-form-item>
                </el-col>
                <el-col>
                    <el-form-item :label="'中文'" prop="zhDict">
                        <el-input v-model="dialogForm.zhDict" type="textarea" :placeholder="$t('public.inputPl')" clearable />
                    </el-form-item>
                </el-col>
                <el-col>
                    <el-form-item :label="'英文'" prop="enDict">
                        <el-input v-model="dialogForm.enDict" type="textarea" :placeholder="$t('public.inputPl')" clearable />
                    </el-form-item>
                </el-col>
                <el-col>
                    <el-form-item :label="'中英文'" prop="zhandenDict">
                        <el-input v-model="dialogForm.zhandenDict" type="textarea" :placeholder="$t('public.inputPl')" clearable />
                    </el-form-item>
                </el-col>
                <el-col>
                    <el-form-item :label="'越南文'" prop="ynDict">
                        <el-input v-model="dialogForm.ynDict" type="textarea" :placeholder="$t('public.inputPl')" clearable />
                    </el-form-item>
                </el-col>
                <el-col>
                    <el-form-item :label="'马来文'" prop="mlDict">
                        <el-input v-model="dialogForm.mlDict" type="textarea" :placeholder="$t('public.inputPl')" clearable />
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm" :loading="loading" icon="el-icon-document" size="small">{{$t('public.save')}}</el-button>
        </div>
    </el-dialog>
</template>

<script>
import {listAllFlawProjectMessage} from '@/api/basic/gaugeMessage'
import {addProject, updateProject} from '@/api/basic/project'
import {listALLFactory} from '@/api/basic/factory'
import EnjoySelectUser from '@/components/EnjoySelectUser'
import EnjoySelectDeptTree from '@/components/EnjoySelectDeptTree'
import FileUploadInput from '@/components/FileUploadInput'
import {languageDictAdd,languageDictUpdate} from '@/api/multilingualMaintenance'

export default {
    name: 'flaw-detial',
    components: { EnjoySelectUser, EnjoySelectDeptTree, FileUploadInput },
    dicts: [
        'NYIQC_PROJECT_TYPE', //量具类型
    ],
    props: {
        value: {
            type: Object,
        },
        visible: {
            type: Boolean,
            default: false,
        },
        type: [String, Number], //1:新增  2：编辑 3：查看
    },
    watch: {
        value(val) {
            this.dialogForm = { ...val }
        },
    },
    computed: {
        isShow: {
            get() {
                return this.visible
            },
            set() {},
        },
        headerTitle() {
            const { type } = this
            let title = ''
            if (type == 1) {
                title =
                    this.$t('public.add')
            }
            if (type == 2) {
                title =
                    this.$t('public.edit')
            }
            return title
        },
    },
    data() {
        return {
            loading: false,
            fullscreen: false,
            deviceOptions: undefined,
            factoryOptions: undefined,
            dialogForm: {},
            // 表单校验
            rules: {
                indexType: [
                    {
                        required: true,
                        message: '请输入',
                        trigger: 'blur',
                    },
                ],
                zhDict: [
                    {
                        required: true,
                        message: '请输入',
                        trigger: 'blur',
                    },
                ],
                enDict: [
                    {
                        required: true,
                        message: '请输入',
                        trigger: 'blur',
                    },
                ],
                zhandenDict: [
                    {
                        required: true,
                        message: '请输入',
                        trigger: 'blur',
                    },
                ],
                // ynDict: [
                //     {
                //         required: true,
                //         message: this.$t('formValidation.inspectionItemNameCannotBeEmpty'),
                //         trigger: 'blur',
                //     },
                // ],
                // mlDict: [
                //     {
                //         required: true,
                //         message: this.$t('formValidation.inspectionItemNameCannotBeEmpty'),
                //         trigger: 'blur',
                //     },
                // ],
            },
        }
    },
    methods: {
        /** 发起提交 */
        submitForm: function () {
            this.$refs.detailForm.validate((valid) => {
                let action = this.type == 1 ? languageDictAdd : languageDictUpdate
                if (valid) {
                    this.loading = true
                    action(this.dialogForm).then(
                        (res) => {
                            this.loading = false
                            if (res.code == 200) {
                                this.$emit('refresh')
                                this.$modal.msgSuccess('操作成功!')
                                this.$emit('update:visible', false)
                            }
                        },
                        (err) => {
                            this.loading = false
                        }
                    )
                }
            })
        },
        handleClose(done) {
            this.$emit('update:visible', false)
            this.$refs.detailForm.resetFields()
        },
        //设备模糊查询
        deviceRemoteMethod(val, cb) {
            console.log(val)
            listAllFlawProjectMessage({ deviceName: val }).then((res) => {
                this.deviceOptions = res.data.map((i) => i.deviceName)
                cb(res.data)
            })
        },
        handleDeviceSelect(e) {
            this.dialogForm.deviceName = e.deviceName
            this.dialogForm.gaugeType = e.gaugeType
            this.$refs.deviceName.$emit('el.form.blur')
        },
        handleDeviceBlur(e) {
            const { deviceOptions, dialogForm } = this
            if (!deviceOptions.includes(dialogForm.deviceName)) {
                this.dialogForm.deviceName = ''
            }
        },
        //工厂模糊查询
        factoryRemoteMethod(val, cb) {
            listALLFactory({ factoryName: val }).then((res) => {
                this.factoryOptions = res.data.map((i) => i.deviceName)
                cb(res.data)
            })
        },
        handleFactorySelect(e) {
            this.dialogForm.factoryName = e.factoryName
            this.$refs.factoryName.$emit('el.form.blur')
        },
        handleFactoryBlur(e) {
            const { factoryOptions, dialogForm } = this
            if (!factoryOptions.includes(dialogForm.factoryName)) {
                this.dialogForm.factoryName = ''
            }
        },
        handleExpireTime(e) {
            console.log(e)
            const today = new Date().getTime()
            const val = new Date(e).getTime()
            if (val > today) {
                this.dialogForm.remDay = ~~(
                    (val - today) /
                    1000 /
                    60 /
                    60 /
                    24
                ).toFixed(1)
            } else {
                this.dialogForm.remDay = 0
            }
            this.$forceUpdate()
        },
        handleRespName(e) {
            this.dialogForm.respName = e.name
        },
        handleEmployName(e) {
            this.dialogForm.employName = e.name
        },
        handleRespDept(e) {
            this.dialogForm.respDept = e.name
        },
    },
}
</script>

<style scoped>
</style>
